import java.util.*;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2021/12/1
 * \* Time: 15:03
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */

class  MyArrayList<E>{
    private Object[] elementData;
    private int usedSize;//代表有效的数据个数

    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    public MyArrayList(){
        this.elementData=DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    public  MyArrayList(int capacity){
        //对参数进行判断
        if(capacity>0){
            this.elementData=new Object[capacity];
        }else if(capacity==0){
            this.elementData=new Object[0];
        }else {
            throw new IllegalStateException("初始化容量不能为负数");
        }
    }

    /**
     * 添加元素,相当于存放在数组的最后位置
     * @param e
     * @return
     */
    public boolean add(E e)
    {
        //确定一个真正的容量
        ensureExplicitCapacity(usedSize+1);
        elementData[usedSize++]=e;
        return true;
    }

    //private void ensureExplicitCapacity(int minCapacity){
        //计算出需要的容量
//        int capacity=calculateCapacity(elementData,minCapacity);
//        //拿着计算出的容量，去看，如果满了，扩容，空的，也是
//        //ensureExplicitCapacity(capacity);
//    }

    private void ensureExplicitCapacity(int minCapacity) {
        //进不去if语句，说明数组还没有放满
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            //扩容
            grow(minCapacity);
    }

    private static final int MAX_ARRAY_SIZE=Integer.MAX_VALUE-8;
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
                Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
    }

    private static int calculateCapacity(Object[] elementData, int minCapacity){
        //1.是否之前elementData数组分配过大小
        if(elementData==DEFAULTCAPACITY_EMPTY_ELEMENTDATA){
            return Math.max(10,minCapacity);
        }
        //2.如果分配过了，则返回+1后的值
        return minCapacity;
    }
}
public class TestDemo2 {

    public static void main(String[] args) {

    }

    public static void main6(String[] args) {
        ArrayList<String> list2=new ArrayList<>();
        //add方法默认是放到数组的最后一个位置
        list2.add("a");
        list2.add("b");
        list2.add("c");
        System.out.println(list2);
        String ret=list2.remove(0);
        System.out.println(ret);
        System.out.println(list2);
        //指定位置：
        /*list2.add(0,"mrzhu");
        System.out.println(list2);

        ArrayList<String> list3=new ArrayList<>();
        list3.add("我是测试1");
        list3.add("我是测试2");
        list3.add("我是测试3");
        list2.addAll(list3);
        System.out.println(list2);*/
    }

    public static void main4(String[] args) {
        ArrayList<String> list1=new ArrayList<>();
        list1.add("hello");
        list1.add("mrzhu");
        list1.add("hhhhh");
        System.out.println("=========迭代器打印===========");
        Iterator<String> it=list1.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();

        System.out.println("=========迭代器List相关打印===========");
        ListIterator<String> it2=list1.listIterator();
        while (it2.hasNext()){
            System.out.print(it2.next()+" ");
        }
    }

    public static void main3(String[] args) {
        ArrayList<String> list1=new ArrayList<>();
        list1.add("hello");
        list1.add("mrzhu");
        list1.add("hhhhh");
        //适用迭代器注意情况
        Iterator<String> it=list1.iterator();
        while (it.hasNext()){
            //首先需要适用next方法迭代出集合中的元素，然后才能调用remove方法，否则集合可能会因为
            //同一个Iterator remove了多次抛出ava.lang.IllegalStateException异常
            String ret=it .next();
            if(ret.equals("hello")){
                it.remove();
            }else {
                System.out.print(ret+" ");
            }
        }


        System.out.println("=========迭代器List相关打印===========");
        ListIterator<String> it2=list1.listIterator();
        while (it2.hasNext()){
            //首先需要适用next方法迭代出集合中的元素，然后才能调用remove方法，否则集合可能会因为
            //同一个Iterator remove了多次抛出ava.lang.IllegalStateException异常
            String ret2=it2 .next();
            if(ret2.equals("hello")){
                it2.remove();
            }else {
                System.out.print(ret2+" ");
            }
        }



    }

    public static void main2(String[] args) {
        List<String> list=new ArrayList<>();//实例化

        ArrayList<String> list1=new ArrayList<>();
        list1.add("hello");
        list1.add("mrzhu");
        list1.add("hhhhh");
        System.out.println(list1);//以字符串形式进行输出
        System.out.println("==========================");
        for(int i=0;i<list1.size();i++){
            System.out.print(list1.get(i)+" ");
        }
        System.out.println();

        System.out.println("====================");
        for (String s:list1) {
            System.out.print(s+" ");
        }
        System.out.println();

        System.out.println("=========迭代器打印===========");
        Iterator<String> it=list1.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();

        System.out.println("=========迭代器List相关打印===========");
        ListIterator<String> it2=list1.listIterator();
        while (it2.hasNext()){
            System.out.print(it2.next()+" ");
        }

        //适用另外一个ArrayList对list2进行初始化
        ArrayList <String>list2=new ArrayList<>(list1);

    }
}
